<template>
	<view class="">
		<!--  -->
		<ben></ben>
		<!--  -->
		<view class="min30_rl background_f rad1 pad20" style="margin-top: 30rpx;">
			<view class="row">
				<view class="font500 col3 size30" style="width: 5em;">
					收货人
				</view>
				<u--input placeholder="请输入收货人" border="none" v-model="name"></u--input>
			</view>
			<view class="row mintop30">
				<view class="font500 col3 size30" style="width: 5em;">
					联系电话
				</view>
				<u--input placeholder="请输入联系电话" border="none" maxlength="11" v-model="phone"></u--input>
			</view>
		</view>
		<view class="posrel min30_rl" :style="{ top: pageHeight + 'px' }">
			<u-button text="保存" shape="circle" color="#FFAD4D" @click="handleinput">
			</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageHeight: '',
				user_id: '',
				address_id: '',
				type: '',
				name: '',
				phone: ''
			}
		},
		onLoad(op) {
			let app = uni.getSystemInfoSync()
			this.pageHeight = app.windowHeight - 280
			this.type = op.type
			if (this.type == 0) {
				this.address_id = op.id
				this.name = op.username
				this.phone = op.phone
			}
		},
		onShow() {
			//当前页面展示的时候他就执行该生命周期
			const token = uni.getStorageSync('token');
			this.user_id = uni.getStorageSync('user_id');
			if (!token) {
				uni.showToast({
					title: '您还未登录，正在前往登录',
					icon: 'none'
				})
				setTimeout(function() {
					uni.navigateTo({
						url: "/mypackage/user/login"
					})
				}, 1000)
			}
		},
		methods: {
			handleinput() {
				var strTemp = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
				if (!this.phone) { //判断如果手机号为空，提示用户输入手机号
					uni.showToast({
						title: '请输入联系电话',
						icon: 'none'
					})
					return
				} else if (!strTemp.test(this.phone)) { //判断如果手机号为空，提示用户输入手机号
					uni.showToast({
						title: '请输入正确的联系电话',
						icon: 'none'
					})
					return
				} else if (!this.name) { //判断如果手机号为空，提示用户输入手机号
					uni.showToast({
						title: '请输入收货人',
						icon: 'none'
					})
					return
				} else {
					if (this.type == 1) {
						this.getAdd();
					} else {
						this.getModify();
					}

					//第一个参数是 缓存的key值 就是说 这个缓存 存在storage 里面的名字
					// uni.setStorageSync('token', token);
				}
			},
			async getAdd() {
				let {
					code,
					message,
					data
				} = await this.$com.fetch("member/user_add", {
					username: this.name,
					phone: this.phone,
					user_id: this.user_id
				})
				if (code == 200) {
					uni.showToast({
						icon: 'success',
						title: message
					})
					setTimeout(function() {
						uni.navigateBack()
					}, 1000)
				} else {
					uni.showToast({
						icon: 'success',
						title: message
					})
				}
			},
			async getModify() {
				let {
					code,
					message,
					data
				} = await this.$com.fetch("member/add_up", {
					username: this.name,
					phone: this.phone,
					user_id: this.user_id,
					address_id: this.address_id
				})
				if (code == 200) {
					uni.showToast({
						icon: 'success',
						title: message
					})
					setTimeout(function() {
						uni.navigateBack()
					}, 1000)
				} else {
					uni.showToast({
						icon: 'success',
						title: message
					})
				}
			}
		}
	}
</script>

<style scoped lang="less">
	.head_back {}
</style>